
* Set Directory
cd "...Replication Files"

* ASSEMBLY
*********************
** The data in raw form was accessed via the Core Files for the National Center for Charitable Statistics. These annual waves were combined into a file of 8,392,805 observations, some of which were duplicates. After duplicates were removed (with the most recent version of a record kept), this left 6,530,204 observations before cleaning.


* CLEANING
*******************

tab subseccd, m
drop if subseccd!=3

tab level1
drop if level1!="PC"


drop if ruledate==.
gen rule_div=ruledate/100
gen rule_year=int(rule_div)
drop if rule_year==0

gen age=fisyr-rule_year
drop if age<-2
drop if age>1000

drop if fisyr<1990
drop if fisyr>2017 & fisyr<.

xtset ein fisyr

gen statecode = .
replace statecode = 1 if state == "AL"
replace statecode = 2 if state == "AR"
replace statecode = 3 if state == "AZ"
replace statecode = 4 if state == "AK"
replace statecode = 5 if state == "CA"
replace statecode = 6 if state == "CO"
replace statecode = 7 if state == "CT"
replace statecode = 8 if state == "DE"
replace statecode = 9 if state == "DC"
replace statecode = 10 if state == "FL"
replace statecode = 11 if state == "GA"
replace statecode = 12 if state == "HI"
replace statecode = 13 if state == "ID"
replace statecode = 14 if state == "IL"
replace statecode = 15 if state == "IN"
replace statecode = 16 if state == "IA"
replace statecode = 17 if state == "KS"
replace statecode = 18 if state == "KY"
replace statecode = 19 if state == "LA"
replace statecode = 20 if state == "ME"
replace statecode = 21 if state == "MD"
replace statecode = 22 if state == "MA"
replace statecode = 23 if state == "MI"
replace statecode = 24 if state == "MN"
replace statecode = 25 if state == "MS"
replace statecode = 26 if state == "MO"
replace statecode = 27 if state == "MT"
replace statecode = 28 if state == "NE"
replace statecode = 29 if state == "NV"
replace statecode = 30 if state == "NH"
replace statecode = 31 if state == "NJ"
replace statecode = 32 if state == "NM"
replace statecode = 33 if state == "NY"
replace statecode = 34 if state == "NC"
replace statecode = 35 if state == "ND"
replace statecode = 36 if state == "OH"
replace statecode = 37 if state == "OK"
replace statecode = 38 if state == "OR"
replace statecode = 39 if state == "PA"
replace statecode = 40 if state == "RI"
replace statecode = 41 if state == "SC"
replace statecode = 42 if state == "SD"
replace statecode = 43 if state == "TN"
replace statecode = 44 if state == "TX"
replace statecode = 45 if state == "UT"
replace statecode = 46 if state == "VT"
replace statecode = 47 if state == "VA"
replace statecode = 48 if state == "WA"
replace statecode = 49 if state == "WV"
replace statecode = 50 if state == "WI"
replace statecode = 51 if state == "WY"

drop if statecode==.
 

* Variables and Cleaning
*************************************

keep ein fisyr ntee1 ntmaj12 totrev cont progrev invinc netrent salesecn saleothn netincfndrsng netincgaming fundinc grprof othinc exps netinc retearn fundbal ass_boy ass_eoy liab_boy liab_eoy paytax compens numempestimate state fips statecode

gen OurTotal=totrev+ cont+ progrev+ invinc+ netrent+ salesecn+ saleothn+ netincfndrsng+ netincgaming+ fundinc+ grprof+ othinc
gen OthInc2=netrent+ salesecn+ saleothn+ netincfndrsng+ netincgaming+ othinc
gen OurTotal2=totrev+ cont+ progrev+ invinc+ fundinc+ grprof+ OthInc2

gen cont3=cont
replace cont3=0 if cont==.
gen progrev3=progrev
replace progrev3=0 if progrev==.
gen invinc3=invinc
replace invinc3=0 if invinc==.
gen netrent3=netrent
replace netrent3=0 if netrent==.
gen salesecn3=salesecn
replace salesecn3=0 if salesecn==.
gen saleothn3=saleothn
replace saleothn3=0 if saleothn==.
gen netincfndrsng3=netincfndrsng
replace netincfndrsng3=0 if netincfndrsng==.
gen netincgaming3=netincgaming
replace netincgaming3=0 if netincgaming==.
gen fundinc3=fundinc
replace fundinc3=0 if fundinc==.
gen grprof3=grprof
replace grprof3=0 if grprof==.
gen othinc3=othinc
replace othinc3=0 if othinc==.

gen OthInc33=netrent3+ salesecn3+ saleothn3+ netincfndrsng3+ netincgaming3+ othinc3
gen OurTotal3=cont3+ progrev3+ invinc3+ fundinc3+ grprof3+ OthInc33

gen PosRevSample=1
replace PosRevSample=0 if OurTotal3<1

gen HHISample=PosRevSample
replace HHISample=0 if cont3<0
replace HHISample=0 if progrev3<0
replace HHISample=0 if invinc3<0
replace HHISample=0 if fundinc3<0
replace HHISample=0 if grprof3<0
replace HHISample=0 if OthInc33<0


* Log Adjustment
gen ln_totrev=ln(totrev)
gen ln_OurTotal=ln(OurTotal)
gen ln_OurTotal2=ln(OurTotal2)
gen ln_OurTotal3=ln(OurTotal3)
gen ln_cont3=ln(cont3)
gen ln_progrev3=ln(progrev3)
gen ln_invinc3=ln(invinc3)
gen ln_fundinc3=ln(fundinc3)
gen ln_grprof3=ln(grprof3)
gen ln_OthInc33=ln(OthInc33)
gen ln_exps=ln(exps)
gen ln_netinc=ln(netinc)
gen ln_retearn=ln(retearn)
gen ln_fundbal=ln(fundbal)
gen ln_ass_boy=ln(ass_boy)
gen ln_ass_eoy=ln(ass_eoy)
gen ln_liab_boy=ln(liab_boy)
gen ln_liab_eoy=ln(liab_eoy)
gen ln_paytax=ln(paytax)
gen ln_compens=ln(compens)


* % and HHI
gen perc_cont3=cont3/OurTotal3
gen perc_progrev3=progrev3/OurTotal3
gen perc_invinc3=invinc3/OurTotal3
gen perc_fundinc3=fundinc3/OurTotal3
gen perc_grprof3=grprof3/OurTotal3
gen perc_OthInc33=OthInc33/OurTotal3

gen HHI3=(perc_cont3*perc_cont3)+(perc_progrev3*perc_progrev3)+(perc_invinc3*perc_invinc3)+(perc_fundinc3*perc_fundinc3)+(perc_grprof3*perc_grprof3)+(perc_OthInc33*perc_OthInc33)

save "...Data_Cleaned.dta", replace


























 
